import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList: [], // 购物车的数据
  },
  mutations: {
    saveCart(state,val){
      let index = state.cartList.findIndex(item => item.id == val.id && item.ties == val.ties);
      // console.log(index);
      if (index>-1){
        state.cartList[index].buyNum += val.buyNum;
        return
      }
      state.cartList.push(val);
    },
    checkAll(state,bool){ // 全选的函数
      state.cartList.forEach(item => {
        item.check = bool
      })
    },
    delect(state,i){ // 单个删除
      state.cartList.splice(i,1)
    },
    remove(state){ // 批量删除
      state.cartList = state.cartList.filter(item => item.check == false) 
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
